package org.voidness.squaretilesframework.savers;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import org.voidness.oje2d.GLImage;
import org.voidness.oje2d.Utils;
import org.voidness.squaretilesframework.Map;
import org.voidness.squaretilesframework.MapObject;
import org.voidness.squaretilesframework.ObjectTileSet;
import org.voidness.squaretilesframework.TileSet;

/**
 * A saver for an old map format Saves maps in the format: mapName width (in
 * tiles) height (int tiles) 0 0 pathToTileImage 0 pathToObjectImage 0 1
 * pathToTileImage 1 pathToObjectImage ... The second number is whether the tile
 * is walkable or not.
 */
public class SimpleSaver implements SaverInterface {
    /**
     * This method saves a map in a simple text format.
     * 
     * @param mMap The map to save
     * @param mFilename The filename where to save the map
     */
    public void save(Map mMap, String mFilename) {
        try {
            // Open a buffer to the file contents
            BufferedWriter dataFile =
                    new BufferedWriter(new FileWriter(mFilename));

            // Write the map name and the size
            dataFile.write(mMap.getName() + MapSaver.LINEBREAK);
            dataFile.write(mMap.getWidth() + MapSaver.LINEBREAK);
            dataFile.write(mMap.getHeight() + MapSaver.LINEBREAK);

            // Start saving the tiles
            TileSet tileSet = mMap.getTileSet();
            ObjectTileSet objectSet = mMap.getObjectTileSet();
            for (int i = 0; i < mMap.getWidth(); i++) {
                for (int j = 0; j < mMap.getHeight(); j++) {
                    int tileIndex = mMap.tileAt(i, j).getImageIndex();
                    int objectIndex = mMap.tileAt(i, j).getObjectIndex();
                    /*
                     * if (tileIndex > -1 || objectIndex > -1) {
                     */
                    // Location
                    dataFile.write(i + " " + j + " ");

                    // Tile image filename
                    if (tileIndex > -1)
                        dataFile.write(((GLImage) tileSet.elementAt(tileIndex))
                                .getFilename()
                                + " ");
                    else
                        dataFile.write("null" + " ");

                    // Tile walkability
                    dataFile.write(mMap.tileAt(i, j).isWalkable() ? "0" : "1");

                    // Tile object filename
                    if (objectIndex > -1)
                        dataFile
                                .write(" "
                                        + ((MapObject) objectSet
                                                .elementAt(objectIndex))
                                                .getFilename()
                                        + MapSaver.LINEBREAK);
                    else
                        dataFile.write(" " + "null" + MapSaver.LINEBREAK);
                    // }
                }
            }

            // Close the file
            dataFile.close();
        } catch (IOException e) {
            Utils.dump("Error saving map file: " + mFilename, Utils.ERROR);
            System.exit(1);
        }

        // If we got here, some wicked stuff happened
        Utils.dump("Map \"" + mMap.getName() + "\" saved to: " + mFilename,
                Utils.ERROR);
    }
}